package com.code.splider.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.splider.dao.ScoreMapper;
import com.code.splider.model.Score;
import com.code.splider.service.ScoreService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class ScoreServiceImpl implements ScoreService{
	
	@Resource
	private ScoreMapper scoreMapper;

	@Override
	public void saveScore(String jsonText) {
		
		try {
			if (null!=jsonText) {
				//删除全部
				scoreMapper.deleteAll();
				
				JSONObject jsonObject = JSONObject.fromObject(jsonText);
				JSONArray jsonArray = jsonObject.getJSONArray("rs");
		        for (int i = 0; i < jsonArray.size(); i++) {
		        	System.out.println("=====================================");
		        	String teams=jsonArray.getString(i);
		            
		            JSONObject jsonObject2 = JSONObject.fromObject(teams);
		            String status = jsonObject2.getString("status");
		            System.out.println("..........状态:"+ status);
		            
		            if (!status.equals("-1")&&!status.equals("全")) {
		            	Score score=new Score();
		            	score.setStatus(status);
		            	
		            	String id = jsonObject2.getString("id");
		            	score.setScoreId(id);
		            	System.out.println("..........id:"+ id);
		            	
		            	String league = jsonObject2.getString("league");
		                JSONObject jsonObject_league = JSONObject.fromObject(league);
		                String n = jsonObject_league.getString("n");
		                score.setCompetition(n);
		                System.out.println("..........赛事:"+ n);
		                
		                String host = jsonObject2.getString("host");
		                JSONObject jsonObject_host = JSONObject.fromObject(host);
		                String n_host = jsonObject_host.getString("n");
		                score.setHost(n_host);
		                System.out.println("..........主队:"+ n_host);
		                
		                if (jsonObject_host.containsKey("p")) {
		                	 String host_p = jsonObject_host.getString("p");
				             score.setHostP(host_p);
				             System.out.println("..........主队排名:"+ host_p);
						}
		               
		                String guest = jsonObject2.getString("guest");
		                JSONObject jsonObject_guest = JSONObject.fromObject(guest);
		                String n_guest = jsonObject_guest.getString("n");
		                score.setGuest(n_guest);
		                System.out.println("..........客队:"+ n_guest);
		                
		                if (jsonObject_guest.containsKey("p")) {
		                	 String guest_p = jsonObject_guest.getString("p");
				             score.setGuestP(guest_p);
				             System.out.println("..........客队排名:"+ guest_p);
						}
		                
		                String sd = jsonObject2.getString("sd");
		                JSONObject jsonObject_sd = JSONObject.fromObject(sd);
		                String f = jsonObject_sd.getString("f");
		                
		                JSONObject jsonObject_f = JSONObject.fromObject(f);
		                
		                if (jsonObject_f.containsKey("hrf")) {
		                	String hrf_sd = jsonObject_f.getString("hrf");
			            	score.setConcedePoints(hrf_sd);
			            	System.out.println("..........初让球:"+ hrf_sd);
			            	
			            	if (jsonObject_f.containsKey("hdx")) {
			            		String hdx_sd = jsonObject_f.getString("hdx");
			                    score.setSizeBall(hdx_sd);
			                    System.out.println("..........初大小球:"+ hdx_sd);
							}
						}
		            	
		            	
		            	if (jsonObject2.containsKey("f_ld")) {
		            		String f_ld = jsonObject2.getString("f_ld");
			            	JSONObject jsonObject_f_ld = JSONObject.fromObject(f_ld);
			            	
			            	if (jsonObject_f_ld.containsKey("hrf")) {
			            		String hrf = jsonObject_f_ld.getString("hrf");
				                score.setConcedePointsNew(hrf);
				                System.out.println("..........即时让球:"+ hrf);
							}
			            	
			            	if (jsonObject_f_ld.containsKey("hdx")) {
			            		String hdx = jsonObject_f_ld.getString("hdx");
				                score.setSizeBallNew(hdx);
				                System.out.println("..........即时大小球:"+ hdx);	
							}
			            	
			            	if (jsonObject_f_ld.containsKey("hrfsp")) {
			            		String hrfsp = jsonObject_f_ld.getString("hrfsp");
				                score.setHrfsp(hrfsp);
				                System.out.println("..........主队即时让球赔率:"+ hrfsp);
							}
			            	
			            	if (jsonObject_f_ld.containsKey("grfsp")) {
		            		    String grfsp = jsonObject_f_ld.getString("grfsp");
				                score.setGrfsp(grfsp);
				                System.out.println("..........客队即时让球赔率:"+ grfsp);
							}
			            	if (jsonObject_f_ld.containsKey("hdxsp")) {
			            		String hdxsp = jsonObject_f_ld.getString("hdxsp");
				                score.setHdxsp(hdxsp);
				                System.out.println("..........大球即时赔率:"+ hdxsp);
							}
			            	if (jsonObject_f_ld.containsKey("gdxsp")) {
			            		String gdxsp = jsonObject_f_ld.getString("gdxsp");
				                score.setGdxsp(gdxsp);
				                System.out.println("..........小球即时赔率:"+ gdxsp);
							}      
						}
		            	
		                if (status.equals("未")) {
		                	 String rtime = jsonObject2.getString("rtime");
		                	 score.setRtime(rtime);
			                 System.out.println("..........开始时间:"+ rtime);
		 				}else{
		 					if (jsonObject2.containsKey("plus")) {
			 					String plus = jsonObject2.getString("plus");
			                    JSONObject jsonObject_plus = JSONObject.fromObject(plus);
			                    
			                    if (jsonObject_plus.containsKey("ha")) {
			                    	String ha_plus = jsonObject_plus.getString("ha");
				                    score.setHaPlus(ha_plus);
				                    System.out.println("..........主队进攻:"+ ha_plus);
								} 
			                    
			                    if (jsonObject_plus.containsKey("ga")) {
			                    	String ga_plus = jsonObject_plus.getString("ga");
				                    score.setGaPlus(ga_plus);
				                    System.out.println("..........客队进攻:"+ ga_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("hd")) {
			                    	 String hd_plus = jsonObject_plus.getString("hd");
					                    score.setHdPlus(hd_plus);
					                    System.out.println("..........主队危险进攻:"+ hd_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("gd")) {
			                    	String gd_plus = jsonObject_plus.getString("gd");
				                    score.setGdPlus(gd_plus);
				                    System.out.println("..........客队危险进攻:"+ gd_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("hso")) {
			                    	String hso_plus = jsonObject_plus.getString("hso");
				                    score.setHsoPlus(hso_plus);
				                    System.out.println("..........主队射正球门:"+ hso_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("gso")) {
			                    	String gso_plus = jsonObject_plus.getString("gso");
				                    score.setGsoPlus(gso_plus);
				                    System.out.println("..........客队射正球门:"+ gso_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("hsf")) {
			                    	String hsf_plus = jsonObject_plus.getString("hsf");
				                    score.setHsfPlus(hsf_plus);
				                    System.out.println("..........主队射偏球门:"+ hsf_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("gsf")) {
			                    	String gsf_plus = jsonObject_plus.getString("gsf");
				                    score.setGsfPlus(gsf_plus);
				                    System.out.println("..........客队射偏球门:"+ gsf_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("hqq")) {
			                    	String hqq_plus = jsonObject_plus.getString("hqq");
				                    score.setHqqPlus(hqq_plus);
				                    System.out.println("..........主队球权:"+ hqq_plus);
								}
			                    
			                    if (jsonObject_plus.containsKey("gqq")) {
			                    	String gqq_plus = jsonObject_plus.getString("gqq");
				                    score.setGqqPlus(gqq_plus);
				                    System.out.println("..........客队球权:"+ gqq_plus);
								}
			                    
			                    if (jsonObject2.containsKey("rd")) {
			                    	String rd = jsonObject2.getString("rd");
				                    JSONObject jsonObject_rd = JSONObject.fromObject(rd);
				                    
				                    if (jsonObject_rd.containsKey("hg")) {
				                    	 String hg = jsonObject_rd.getString("hg");
						                    score.setHg(hg);
						                    System.out.println("..........主队得分:"+ hg);
									}
				                    
				                    if (jsonObject_rd.containsKey("gg")) {
				                    	 String gg = jsonObject_rd.getString("gg");
						                    score.setGg(gg);
						                    System.out.println("..........客队得分:"+ gg);
									}
				                    
				                    if (jsonObject_rd.containsKey("hc")) {
				                    	 String hc = jsonObject_rd.getString("hc");
						                    score.setHc(hc);
						                    System.out.println("..........主队角球:"+ hc);
									}
				                    
				                    if (jsonObject_rd.containsKey("gc")) {
				                    	String gc = jsonObject_rd.getString("gc");
					                    score.setGc(gc);
					                    System.out.println("..........客队角球:"+ gc);
									}
								}  
						    } 
		 			    } 
		                score.setUpdateTime(new Date());
		                score.setIsDelete(true);
		                scoreMapper.insert(score);
					}
		        }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}